<?php
class M_student_grades extends MY_Model {
	
	private $__table = 'student_grades';
	
	function generate_student_grades($enrollment_id, $section_id){
		$this->load->model('M_student_subjects');
		$this->load->model('M_gperiods');
		$student_subjects = $this->M_student_subjects->all_by_enrollment_id($enrollment_id);
		$grading_periods = $this->M_gperiods->get_gperiods();
	  
	  if(!empty($student_subjects)){
	  
	    foreach($student_subjects as  $id => $r):
	     foreach($grading_periods as $key=>$row):
	      $data[$id.'-'.$key]['subject_id'] = $r->subject_id;
	      $data[$id.'-'.$key]['enrollment_id'] = $enrollment_id;
	      $data[$id.'-'.$key]['grading_period_id'] =$row->gp_id;
	     endforeach;
	    endforeach;
	    
	    $this->db->where('enrollment_id',$enrollment_id)->delete($this->__table);
	    $this->db->insert_batch($this->__table, $data); 
	  }
	  
	}
	
	function grouped_subject_by_enrollment_id($enrollment_id)
	{
	  // $sql = "SELECT subjects.subj_code,subjects.subj_id
   //          FROM student_grades
   //          LEFT JOIN enrollments e ON e.e_id = student_grades.enrollment_id
   //          LEFT JOIN subjects ON subjects.subj_id = student_grades.subject_id
   //          LEFT JOIN grading_periods ON grading_periods.gp_id = student_grades.grading_period_id
   //          WHERE student_grades.enrollment_id = ?
   //          AND e.adviser_show_grades = 1
   //          AND e.finance_show_grades = 1
   //          GROUP BY student_grades.subject_id";

	  // $query = $this->db->query($sql, $enrollment_id);
	  // return $query->num_rows() > 0 ? $query->result() : FALSE;

	  $sql = "	SELECT subjects.subj_code,subjects.subj_id
				FROM student_grades
				LEFT JOIN enrollments e ON e.e_id = student_grades.enrollment_id
				LEFT JOIN subjects ON subjects.subj_id = student_grades.subject_id
				LEFT JOIN grading_periods ON grading_periods.gp_id = student_grades.grading_period_id
				WHERE student_grades.enrollment_id = ?
				AND e.adviser_show_grades = 1
				AND e.finance_show_grades = 1
				GROUP BY student_grades.subject_id";
	  
	  $query = $this->db->query($sql, $enrollment_id);
	  return $query->num_rows() > 0 ? $query->result() : FALSE;
	}
	
	function get_all_by_enrollment_id($enrollment_id){
	  
	  $sql = "SELECT subject_id, grading_period_id, value, remarks
            FROM student_grades
            WHERE student_grades.enrollment_id = ?
			AND ay_id = ?";
	  
	  $query = $this->db->query($sql,array($enrollment_id,$this->c_sy->id));
		return $query->num_rows() > 0 ? $query->result() : FALSE;
	}

	public function get_student_remarks($id = '')
	{
		$sql = 'SELECT remarks FROM student_remarks
				  LEFT JOIN enrollments ON enrollments.e_id = student_remarks.e_id
				  WHERE student_remarks.e_id = ? 
				  AND student_remarks.ay_id = ?
				  AND enrollments.finance_show_grades = 1
				  AND enrollments.adviser_show_grades = 1';
		$r = $this->db->query($sql,[$id,$this->c_sy->id]);
		return $r->num_rows() >=1 ? $r->result() : FALSE;
	}
	
	public function get_grading_system()
	{
		$q = $this->db->get('grading_system');
		
		if($q->num_rows() >= 1)
		{
			foreach($q->result() as $k => $v)
			{
				$data[$v->varname]['rangefrom'] = $v->range_start;
				$data[$v->varname]['rangeto'] = $v->range_end;
				$data[$v->varname]['id'] = $v->id;
			}
			

			return $data;
			
		}else{
			return FALSE;
		}
		// return $q->num_rows() >=1 ? $q->result() : FALSE;
	}
}
?>
